﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MainViewModel.cs" company="OxyPlot">
//   Copyright (c) 2014 OxyPlot contributors
// </copyright>
// <summary>
//   Represents the view-model for the main window.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace OxyPlotTest
{
    using OxyPlot;
    using OxyPlot.Annotations;
    using OxyPlot.Series;
    using System;

    /// <summary>
    /// Represents the view-model for the main window.
    /// </summary>
    public class MainViewModel
    {
        private LineAnnotation m_cursor1Annotation; // 游标1

        /// <summary>
        /// Initializes a new instance of the <see cref="MainViewModel" /> class.
        /// </summary>
        public MainViewModel()
        {
            // Create the plot model
            var tmp = new PlotModel { Title = "Simple example", Subtitle = "using OxyPlot" };

            // Create two line series (markers are hidden by default)
            var series1 = new LineSeries { Title = "Series 1", MarkerType = MarkerType.Circle };
            series1.Points.Add(new DataPoint(0, 0));
            series1.Points.Add(new DataPoint(10, 18));
            series1.Points.Add(new DataPoint(20, 12));
            series1.Points.Add(new DataPoint(30, 8));
            series1.Points.Add(new DataPoint(40, 15));

            var series2 = new LineSeries { Title = "Series 2", MarkerType = MarkerType.Square };
            series2.Points.Add(new DataPoint(0, 4));
            series2.Points.Add(new DataPoint(10, 12));
            series2.Points.Add(new DataPoint(20, 16));
            series2.Points.Add(new DataPoint(30, 25));
            series2.Points.Add(new DataPoint(40, 5));


            // Add the series to the plot model
            tmp.Series.Add(series1);
            tmp.Series.Add(series2);

            /* 游标1 */
            m_cursor1Annotation = new LineAnnotation()
            {
                Color = OxyColors.Red,
                X = 10,
                LineStyle = LineStyle.Dash,
                Type = LineAnnotationType.Vertical,
                Text = ""
            };
            tmp.Annotations.Add(m_cursor1Annotation);

            tmp.MouseMove += Model_MouseMove;

            // Axes are created automatically if they are not defined

            // Set the Model property, the INotifyPropertyChanged event will make the WPF Plot control update its content
            this.Model = tmp;
        }

        private void Model_MouseMove(object sender, OxyMouseEventArgs e)
        {
            //throw new NotImplementedException();
            HitTestResult result = m_cursor1Annotation.HitTest(new HitTestArguments(e.Position, 10));
            if (result != null)
            {
                if (string.IsNullOrEmpty(m_cursor1Annotation.Text))
                {
                    m_cursor1Annotation.Text = "Cursor1";
                    Model.InvalidatePlot(false);
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(m_cursor1Annotation.Text))
                {
                    m_cursor1Annotation.Text = "";
                    Model.InvalidatePlot(false);
                }
            }
        }

        /// <summary>
        /// Gets the plot model.
        /// </summary>
        public PlotModel Model { get; private set; }
    }
}